<?php

class RoleController  extends  Controller{
	
	public function index(){
		$this->getSmarty();
		if(empty( $_SESSION["template"])){
			$_SESSION["template"]="default";
		}
		$this->smarty->display("admin/role.tpl");
	}
	
	public function roleList(){
		
		//页码
		$page = $_POST["page"];
		if($page == null ){
			$page = $_GET["page"];
		}
		//每页显示记录数
		$rows = $_POST["rows"];
		if($rows == null ){
			$rows = $_GET["rows"];
		}
		$start = ($page-1)*$rows ;
		$end = $page*$rows;
		//排序索引id
		$sidx = $_POST["sidx"];
		if($sidx == null ){
			$sidx = $_GET["sidx"];
		}
		//排序顺序
		$sord = $_POST["sord"];
		if($sord == null ){
			$sord = $_GET["sord"];
		}
		
		$dbutil = $this->getDB();
		
		$res = $dbutil->get_results(" select count(role.role_id) total from 
				tb_roles role LEFT JOIN admin_users user on user.id = role.role_create_user_id ");
		$records = $res;
		foreach ($res as $re){
			$records = $re->total;
			break;
		}
		
		$sql = " select role.role_id,role.role_name,role.role_desc,role.role_create_date,user.user_name user_name ".
			" from tb_roles role LEFT JOIN admin_users user on user.id = role.role_create_user_id ";
		if($sidx != null && $sidx != "" && $sord != null && $sord != ""){
			$sql = $sql." order by role.$sidx $sord ";
		}
		$sql = $sql." limit $start ,$rows";
		
		$res = $dbutil->get_results($sql);
		$data = array();
		foreach ($res as $re){
			$data[] = array(
					"id"=>$re->role_id,
					"cell"=>array(
							$re->role_id,
							$re->role_name,
							$re->role_desc,
							$re->role_create_date,
							$re->user_name)
			);
		}
		
		$totalPage = ceil($records/$rows);
		
		$result = array(
				"rows"=>$data,
				"page"=>$page,
				"total"=>$totalPage,
				"records"=>$records);
		
		echo json_encode($result);
	}
	
	function delRole(){
		
		$roleId = $_POST["role_id"];
		
		$dbutil = $this->getDB();
		$dbutil->query(" delete from tb_role_menu where role_id=$roleId");
		
		$dbutil->query(" delete from tb_roles where role_id=$roleId");
		
		$result = array(
				success=>true,
		);
		echo json_encode($result);
		
	}
	
	function addRole(){
		
		$roleId =  $_POST["roleId"];
		$roleName = $_POST["roleName"];
		$roleDesc = $_POST["roleDesc"];
		$menu = $_POST["menu"];
		
		$dbutil = $this->getDB();
		
		$userInfo = $_SESSION['aduser'];
		$admin_id = $userInfo->id;
		
		//获取roleid
		$role_id = null ;
		if($roleId == null || "" == $roleId){
			$res = $dbutil->get_results("select MAX(role_id) ids from tb_roles ");
			foreach ($res as $re){
				$role_id = 1+$re->ids;
				break;
			}
			if($role_id == null){
				$role_id = 0;
			}
			
			//插入角色
			$dbutil->query(" insert into tb_roles(role_id,role_name,role_desc,role_create_user_id,role_create_date) 
					values($role_id,'$roleName','$roleDesc','$admin_id',sysdate()) ");
		}else{
			$role_id = $roleId;
			//更新角色
			$dbutil->query(" update tb_roles set role_name='$roleName',role_desc='$roleDesc',
					role_create_user_id='$admin_id',role_create_date=sysdate() where role_id=$roleId");
			
			//删除角色菜单关联
			$dbutil->query(" delete from tb_role_menu where role_id=$role_id");
		}
			
		//插入菜单与角色关联
		if($menu != null && $menu != ""){
			$menus = explode(",",$menu);
			foreach ($menus as $me){
				
				$data = array(
						menu_id=>$me,
						role_id=>$role_id
				);
				$dbutil->insert("tb_role_menu",$data );
			}	
		}
		
		$result = array(
			success=>true,
		);
		echo json_encode($result);
	}
	
}

?>